import checkRule from '@/views/modules/form/customForm/js/checkRule'
export default {
    mixins:[checkRule],
    methods:{
        i18n_(name, text, key) {
            return this.$ti18(name, text, "formComponentsLang", key);
        },
        /**
         * 验证必填
         * @param rulesItem 当前规则
         * @param item 控件配置
         * @param v 当前值
         * @param name 提示语句
         * @param permission 权限
         * @param row 当前数据
         * @returns {boolean|{msg: string, state: boolean}|{msg: string, success: boolean}}
         */
        reqMsg(rulesItem, item, v, name, permission, row) {
            let isShow = true
            if (item.type == "hidden") {
                return false
            }

            let {conditionConfig} = item.control;
            if (!conditionConfig) {
                return true
            }
            if (conditionConfig.condition) {
                let {condition} = conditionConfig;
                if (condition.indexOf('return') === -1) {
                    condition = 'return ' + condition;
                }
                isShow = new Function("row", condition).call(this.formVm?this.formVm:this, row);
            }
            if (!isShow) {
                //隐藏控件直接通过
                return {success: true, msg:this.i18n_('verification','校验通过')}
            }
            let isCheckRequired = this.isCheckRequired();
            if (permission == 'required' && this.isValNull(v, item.type) && isCheckRequired) {
                return {success: false, msg: name + this.i18n_('required','必填')};
            }
            if (rulesItem.condition) {
                //根据js判断必填
                let condition = rulesItem.condition
                if (!condition.includes('return')) {
                    condition = `return ${condition}`;
                }
                let required = new Function("row", condition).call(this.formVm?this.formVm:this, row);
                if (required && this.isValNull(v, item.type) && isCheckRequired) {
                    return {success: false, msg: name + this.i18n_('required','必填')};
                }
            }
            if (rulesItem.required && this.isValNull(v,item.type) && !rulesItem.condition  && isCheckRequired) {
                return {success: false, msg: name + this.i18n_('required','必填')};
            }
            //校验长度
            if (rulesItem.max != undefined && rulesItem.max != 0) {
                let _max = rulesItem.max + '';
                if ((['upload', 'image', 'textarea'].includes(item.type)) && item.control.type === 'clob') {
                    //上传附件或图片大文本 不验证字段长度；
                    return {success: true, msg: this.i18n_('verification','校验通过')}
                }
                if (_max.includes('.') && typeof v === 'number') {
                    let value = v + '';
                    //如果长度有小数点
                    let maxAr = _max.split('.');
                    let rule1 = maxAr[0], rule2 = maxAr[1];
                    let _valueArr = value.split('.');
                    if (!this.isValNull(value, item) && _valueArr[1] && _valueArr[1].length > rule2) {
                        return {state: false, msg: this.i18n_('numberTips','数字小数位超出！')}
                    } else if (!this.isValNull(value) && _valueArr[0].length > (Number(rule1) - Number(rule2))) {
                        //如果长度设置为零 则没有长度限制
                        return {state: false, msg: name + this.i18n_('exceed','超过')  + parseInt(rulesItem.max)}
                    }
                } else if (!this.isValNull(v) && this.changeStrLength(v, item) > rulesItem.max) {
                    //如果长度设置为零 则没有长度限制
                    return {state: false, msg: name + this.i18n_('exceed','超过') + parseInt(rulesItem.max)}
                }
            }
            if (rulesItem.pattern) {
                let regular = new RegExp(rulesItem.pattern, 'g');
                //正则表达式数据不为空的时候才校验 ;
                if (!this.isValNull(v) && !regular.test(v)) {
                    return {success: false, msg: name + rulesItem.message};
                }
            }
            //地址控件
            if (item.type == "address" && rulesItem.required) {
                let msgObj = {
                    province:this.i18n_('province','省'),
                    city:this.i18n_('city','市'),
                    county:this.i18n_('county','县/区'),
                    address:this.i18n_('address','详细地址'),
                }
                let {isCity, isCounty, isAddress} = item.options;
                let ary = ['province', 'city', 'county', 'address'];
                if (!isCity) {
                    ary = ['province'];
                } else if (!isCounty) {
                    ary = ['province', 'city'];
                } else if (!isAddress) {
                    ary = ['province', 'city', 'county'];
                }
                if (this.isValNull(v)   && isCheckRequired) {
                    return {success: false, msg: name + this.i18n_('required','必填')};
                }
                let val = new Function(`return ${v}`)();
                let msgList = [];
                for (let [index, key] of ary.entries()) {
                    if ((this.isValNull(v) || !val[key]) && msgObj[key]) {
                        msgList.push(msgObj[key]);
                    }
                }
                if (msgList.length > 0   && isCheckRequired) {
                    return {
                        success: false,
                        msg: name + msgList.join("、") + this.i18n_('required','必填')
                    }
                }
            }
            return {success: true, msg: this.i18n_('verification','校验通过')};
        },
        /**
         * 判断值是否为空
         * @param val 当前控件的值
         * @param type 当前控件的类型
         * @returns {boolean} 如果值为空则返回true
         */
        isValNull(val, type) {
            //判断值是否为空,为空返回true
            let value = typeof val === 'string' ? val.trim() : val;
            if (value || (value === 0 && type !== 'score')) {
                //控件为0也算有值，但是评分控件除外
                if (Array.isArray(value) && value.length === 0) {
                    return true;
                }
                return false;
            }
            return true;
        },
        isCheckRequired(){
            //如果checkRequired：false则不校验必填
            if((typeof this.checkRequired === "boolean" && this.checkRequired) || (this.checkRequired === undefined)) {
                return true;
            }
            return false;
        },
    }
}
